<%@ tag language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ attribute name="pagesize" required="true"%>
<%@ tag import="java.util.Date" import="java.text.DateFormat"
	import="java.util.List" import="ro.k.web.beans.order.ReqManoperaBean"%>
<jsp:useBean id="tagReqMan" class="ro.k.tags.ReqManopera"
	scope="session" />

<%
	final String PAGE_PARAM = "prqm";
	final String REM_ELEM = "rmelm";

	int currPage = ro.k.web.beans.KBeanUtils.pageNr(request
			.getParameter(PAGE_PARAM));

	String page = request.getRequestURI().substring(
			request.getRequestURI().lastIndexOf("/") + 1);

	int pgsize = Integer.parseInt(pagesize);

	tagReqMan.remove(request.getParameter(REM_ELEM));

	List<ReqManoperaBean> li = tagReqMan.getReqMans();

	int nrPages = li.size() / pgsize;
	if (li.size() % pgsize != 0 || nrPages == 0) {
		nrPages++;
	}
%>
<h3>Manopera ceruta de client</h3>
<table>
	<tbody>
		<tr>
			<td>
				<form action="<%=request.getContextPath()%>/addReqMan" method="post">
					<table>
						<thead>
							<tr>
								<th>Denumire</th>
								<th>Valoare</th>
								<th>Durata (h)</th>
								<th>&nbsp;</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td><input type="text" name="denumire" title="Denumire"
									style="width: 400px" id="denumire" onkeyup="validateAdd();" /></td>

								<td><input type="text" name="valoare" title="Valoare"
									onkeydown="return IsNumeric(event, this);"
									onkeyup="validateAdd();"
									style="text-align: right; width: 50px;" id="valoare" /></td>

								<td><input type="text" name="durata" title="Durata"
									onkeydown="return IsNumeric(event, this);"
									onkeyup="validateAdd();"
									style="text-align: right; width: 70px;" id="durata" /> <input
									type="hidden" value="<%=page%>" name="pagina" /></td>

								<td><input type="submit" name="adauga" value="Adauga"
									title="Adauga manopera ceruta" id="addManoperaBtn"></td>
							</tr>
						</tbody>
					</table>
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<table>
					<thead>
						<tr>
							<th width="400px;">Denumire</th>
							<th width="50px;">Valoare</th>
							<th width="70px;">Durata (h)</th>
							<th width="25px;">&nbsp;</th>
						</tr>
					</thead>
					<tbody>
						<%
							int i = (currPage - 1) * pgsize;
							int rowc = (i % 2) == 0 ? 1 : 0;

							for (; i < currPage * pgsize; i++) {
								if (i >= li.size()) {
						%>
						<tr <%out.print(((i + rowc) % 2 == 0) ? "" : "class=\"imp\"");%>>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						</tr>
						<%
							} else {
						%>
						<tr <%out.print(((i + rowc) % 2 == 0) ? "" : "class=\"imp\"");%>>
							<td><%=li.get(i).getDenumire()%></td>
							<td align="right"><%=li.get(i).getValoare()%></td>
							<td align="right"><%=li.get(i).getDurata()%></td>
							<td><a href="<%=page + "?" + REM_ELEM + "=" + i%>"
								style="text-decoration: none;">sterge</a></td>
						</tr>
						<%
							}
							}
						%>
					</tbody>
					<tfoot>
						<tr>
							<td align="center" width="70%">
								<table>
									<tr>
										<%
											if (nrPages > 1) {
										%>
										<td width="33%">
											<%
												if (currPage > 1) {
											%> <a href="<%=page + "?" + PAGE_PARAM + "=1"%>">&lt;&lt;Prima</a>
											<a href="<%=page + "?" + PAGE_PARAM + "=" + (currPage - 1)%>">&lt;Inapoi</a>
											<%
												}
											%>
										</td>

										<td width="<%=(nrPages == 1) ? 45 : 32%>%" align="center">
											<%
												out.println("Pagina " + currPage + " din " + nrPages);
											%>
										</td>

										<td width="35%">
											<%
												if (currPage < nrPages) {
											%> <a
											href="<%=page + "?" + PAGE_PARAM + "=" + (currPage + 1)%>">Inainte&gt;</a>
											<a href="<%=page + "?" + PAGE_PARAM + "=" + nrPages%>">Ultima&gt;&gt;</a>
											<%
												}
											%>
										</td>
										<%
											} else {
										%>
										<td colspan="3" align="center">
											<%
												out.println("Pagina " + currPage + " din " + nrPages);
											%>
										</td>
										<%
											}
										%>
									</tr>
								</table>
							</td>
							<td align="right"><%=tagReqMan.getTotalValoare()%></td>
							<td align="right"><%=tagReqMan.getTotalTime()%></td>
							<td align="right">&nbsp;</td>
						</tr>
					</tfoot>
				</table>
			</td>
		</tr>
	</tbody>
</table>